/**
 * Class wxpay
 * @package
 * 文件功能描述：微信支付
 * 作者：xuliangchd
 * 完成日期：2019/8/10 12:20:18
 */
module.exports = class extends think.Service {
  // 微信小程序支付
  async wxappletPay(data) {
    const Tenpay = require('tenpay');
    const config = {
      appid: think.config('wechat.AppID'),
      mchid: think.config('wechat.mch_id'),
      partnerKey: think.config('wechat.partner_key'),
      notify_url: think.config('wechat.notify_url'),
      refund_url: think.config('wechat.refund_url'),
      spbill_create_ip: this.ip
    };
    const api = new Tenpay(config);
    const result = await api.getPayParams({
      out_trade_no: data['out_trade_no'], // 订单内容
      body: data['body'], // 订单内容
      total_fee: data['total_fee'], // 订单金额(分)
      openid: data['openid'] // 用户OPENID
    });
    return result;
  }
  // 微信小程序退款
  async wxappletRefund(orderNumber, orderMoney, orderRefundNumber) {
    const Tenpay = require('tenpay');
    const pfx = require('fs').readFileSync('pem/apiclient_cert.p12');// 小程序证书路径
    const config = {
      appid: think.config('wechat.AppID'),
      mchid: think.config('wechat.mch_id'),
      partnerKey: think.config('wechat.partner_key'),
      pfx: pfx,
      notify_url: think.config('wechat.notify_url'),
      refund_url: think.config('wechat.refund_url'),
      spbill_create_ip: this.ip
    };
      // console.error(config);
    const api = new Tenpay(config);
    // 'refund_account': 'REFUND_SOURCE_RECHARGE_FUNDS',// 退款方式：余额退款
    const result = await api.refund({
      'out_trade_no': orderNumber,
      'out_refund_no': orderRefundNumber,
      'total_fee': orderMoney,
      'refund_fee': orderMoney
    });
    return result;
  }
};
